package com.growthsee.rider.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.growthsee.common.core.controller.BaseController;
import com.growthsee.common.core.domain.AjaxResult;
import com.growthsee.common.core.page.PageDomain;
import com.growthsee.common.core.page.TableDataInfo;
import com.growthsee.common.utils.bean.BeanUtils;
import com.growthsee.rider.domain.RiderCarsInfo;
import com.growthsee.rider.domain.RiderLoanInfo;
import com.growthsee.rider.domain.RiderPackage;
import com.growthsee.rider.domain.dto.RiderLoanInfoDto;
import com.growthsee.rider.param.BaseParam;
import com.growthsee.rider.service.RiderCarsInfoService;
import com.growthsee.rider.service.RiderLoanInfoService;
import com.growthsee.rider.service.RiderPackageService;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 借款信息 前端控制器
 * </p>
 *
 * @author csy
 * @since 2021-05-25
 */
@RestController
@RequestMapping("/rider/loanInfo")
@AllArgsConstructor
public class RiderLoanInfoController extends BaseController {
    private final RiderLoanInfoService riderLoanInfoService;
    private final RiderCarsInfoService riderCarsInfoService;
    private final RiderPackageService riderPackageService;

    @GetMapping
    public TableDataInfo query(PageDomain page, BaseParam baseParam) {
        LambdaQueryWrapper<RiderLoanInfo> queryWrapper = Wrappers.lambdaQuery();
        if(StringUtils.isNotEmpty(baseParam.getKeywords())){
            queryWrapper.and(wp->wp
                    .like(RiderLoanInfo::getIdCardNo, baseParam.getKeywords())
                    .or()
                    .like(RiderLoanInfo::getUserName, baseParam.getKeywords())
                    .or()
                    .like(RiderLoanInfo::getChannelMerName, baseParam.getKeywords())
                    .or()
                    .like(RiderLoanInfo::getAcctCard, baseParam.getKeywords())
            );
        }
        if(StringUtils.isNotEmpty(baseParam.getStat())){
            queryWrapper.eq(RiderLoanInfo::getStat, baseParam.getStat());
        }
        if(StringUtils.isNotEmpty(baseParam.getCreditStat())){
            queryWrapper.eq(RiderLoanInfo::getCreditStat, baseParam.getCreditStat());
        }
        if(StringUtils.isNotEmpty(baseParam.getLoanStat())){
            queryWrapper.eq(RiderLoanInfo::getLoanStat, baseParam.getLoanStat());
        }
        queryWrapper.orderByDesc(RiderLoanInfo::getCreateTime);
        Page page1 = riderLoanInfoService.page(new Page<>(page.getPageNum(),page.getPageSize()), queryWrapper);
        return getDataTable(page1.getRecords(),page1.getTotal());
    }

    @GetMapping("{id}")
    @ApiOperation(value = "根据id查询题型", notes = "根据id查询题型")
    public AjaxResult findOne(@PathVariable String id) {
        RiderLoanInfo riderQuestionType = riderLoanInfoService.getById(id);
        RiderLoanInfoDto riderLoanInfoDto = new RiderLoanInfoDto();
        List<RiderCarsInfo> list = riderCarsInfoService.list(Wrappers.<RiderCarsInfo>lambdaQuery().eq(RiderCarsInfo::getLoanId, id));
        List<RiderPackage>packages = riderPackageService.list(Wrappers.<RiderPackage>lambdaQuery().eq(RiderPackage::getLoanId, id));
        riderLoanInfoDto.setBuyCarsInfo(list);
        riderLoanInfoDto.setRiderPackages(packages);
        BeanUtils.copyBeanProp(riderLoanInfoDto,riderQuestionType);
        return AjaxResult.success(riderLoanInfoDto);
    }
}

